<template>
    <div class="con">
        <div class="login">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="pass">
                    <el-input type="text" v-model="ruleForm.pass" autocomplete="off" size="small"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" size="small"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="age">
                    <el-input v-model="ruleForm.age" size="small" style="width: 100%;"></el-input>
                    <span v-html="captcha"></span>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')" size="small">提交</el-button>
                    <el-button @click="resetForm('ruleForm')" size="small">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
axios.defaults.withCredentials = true;
export default {
    data() {
        var checkAge = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('年龄不能为空'));
            }
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            }
            else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: '',
                checkPass: '',
                age: ''
            },
            rules: {
                pass: [{
                    validator: validatePass,
                    trigger: 'blur'
                }],
                checkPass: [{
                    validator: validatePass2,
                    trigger: 'blur'
                }]
            },
            captcha: ""
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    await this.$axios.post("/login", {
                        userName: this.ruleForm.pass,
                        pwd: this.ruleForm.checkPass,
                        captcha: this.ruleForm.age
                    }).then(res => {
                        console.log(res);
                        localStorage.setItem('token', res.data.token);
                        this.$router.push('/uploadimg')
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        async getCaptcha() {
            await this.$axios.get("/captcha").then(res => {
                this.captcha = res.data;
            })
        }
    },
    mounted() {
        this.getCaptcha();
    },
}
</script>

<style>
.con {
    background: url("../assets/img/login_bg.jpg");
    display: flex;
    justify-content: flex-end;
}

.login {
    background-color: white;
    width: 300px;
    padding: 100px 60px;
}
</style>
